﻿<%@ WebHandler Language="C#" Class="getImage" %>

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;

public class getImage : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        try
        {

            if (context.Request["h"] != null && context.Request["w"] != null && context.Request["image"] != null && context.Request["color"] != null)
            {

                string strPath = context.Request["image"].ToString();
                string strColor = context.Request["color"].ToString();

                int h = 0, w = 0;

                try
                {
                    h = int.Parse(context.Request["h"].ToString());
                    w = int.Parse(context.Request["w"].ToString());
                }
                catch
                {
                    return;
                }




                System.Drawing.Bitmap bmOld = (Bitmap)Image.FromFile(context.Server.MapPath(strPath));

                Bitmap bmNew = (Bitmap)resizeImage((Image)bmOld, w, h, Color.FromName(strColor));


                System.IO.MemoryStream ms = new System.IO.MemoryStream();

                bmNew.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);




                context.Response.ContentType = "image/jpeg";
                byte[] btNew = ms.ToArray();
                context.Response.OutputStream.Write(btNew, 0, btNew.Length);




            }

        }
        catch
        {
        }
        
         
        
        
        
        
        
        
        
        
    }



    /// <summary>
    /// this function will resize the image and add the color to back ground
    /// </summary>
    /// <param name="imgNew">the image to resize</param>
    /// <param name="NewWidth">the new width</param>
    /// <param name="NewHeight">the new height</param>
    /// <param name="cr">the color of back</param>
    /// <returns>a resized Image</returns>
    public static System.Drawing.Image resizeImage(System.Drawing.Image imgNew, int NewWidth, int NewHeight, Color cr)
    {
        int w = NewWidth;
        int h = NewHeight;

        System.Drawing.Bitmap bmOld = (Bitmap)imgNew;


        if (w == bmOld.Width && h == bmOld.Height)
            return (Image)bmOld;
        
        
        int oldH = bmOld.Height;
        int oldW = bmOld.Width;


        int newH = 0;
        int newW = 0;

        newH = h;
        newW = newH * oldW / oldH;

        if (newW > w)
        {
            newW = w;
            newH = newW * oldH / oldW;
        }




        Bitmap bmNew = new Bitmap(w, h);
        bmNew.SetResolution(bmOld.HorizontalResolution, bmOld.VerticalResolution);
        Graphics grdNew = Graphics.FromImage((Image)bmNew);

        grdNew.CompositingQuality = CompositingQuality.HighQuality;
        grdNew.InterpolationMode = InterpolationMode.HighQualityBicubic;
        grdNew.SmoothingMode = SmoothingMode.HighQuality;

        grdNew.FillRectangle(new SolidBrush(cr), new Rectangle(0, 0, w, h));

        grdNew.DrawImage((Image)bmOld, new RectangleF(0 + ((w - newW) / 2), 0 + ((h - newH) / 2), newW, newH));



        grdNew.Dispose();


        return (Image)bmNew;
    }
    
    
    
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}